package com.ay277.htqoa.staff.dao;

import com.ay277.htqoa.base.dao.BaseDao;
import com.ay277.htqoa.staff.entity.Event;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by hghh on 2016/12/19.
 */
@Repository
public class EventDaoImpl extends BaseDao<Event,String> implements EventDao {
    {
        type = Event.class;
    }
    @Override
    public List<Event> selectEvent(final String start,final String end) {
        return getHibernateTemplate().executeWithNativeSession(new HibernateCallback<List<Event>>() {
            @Override
            public List<Event> doInHibernate(Session session) throws HibernateException {
                Query query = session.createQuery("select e.title as title,e.content as content" +
                        ",e.start as start,e.end as end from Event e where e.start>=:start and e.end <= :end  ");
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    query.setParameter("start",format.parse(start));
                    query.setParameter("end",format.parse(end));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                return query.list();
            }
        });
    }
}
